/**
 *  Depends on:
 *      Notifier.js
 *      jquery.js
 */

/**
 *  Handles server response
 *  @param response defines server-side response as js object
 */
var handleResponse = function(response){
    if(response.status){
        response.result = response.status=='Success';
        if(response.result){
            if(response.message){
                Notifier.success(response.message, response.title);
            }
        } else {
            if(response.message){
                var msg = response.message;
                var title = response.title;
                var stack = null;
                if(response.title.indexOf('ConstraintViolationException')>0){
                    title = 'Some elements depends on this';
                    stack = response.title;
                }
                if(title==stack) {
                    title = msg;
                    msg = stack;
                    stack = null;
                }
                Notifier.customError(msg, title, stack, 15000);
            } else if(response.title){
                Notifier.customError(response.title, null, stack, 15000);
            }
            response = null;
        }

//        if(response.redirectUrl){
//            window.location.href = response.redirectUrl;
//        }
    }
    return response;
}

/**
 *
 *  @param options
 */
function confirmDialog(options){
    var $dlg = $('<div>').addClass('sidebar-container');
    var $sidebar = $('<div>').addClass('sidebar').appendTo($dlg);
    
    // text
    var $msg = $('<div>')
        .css('padding', '15px 15px 0px 15px')
        .text('Do you really want to '+options.text+'?')
        .appendTo($sidebar);
    
    // buttons
    var closeDialog = function(){
        $dlg.dialog('close');
    }
    
    var $yes = uic.element.button({
        title: 'Yes',
        primary:  true,
        classes: 'btn-space',
        onclick: function(){
            options.callback()
            closeDialog();
        }
    }).getElement();
    var $no = uic.element.button({
        title: 'No',
        classes: 'btn-space',
        onclick: closeDialog
    }).getElement();
    
    var $buttons = $('<div>')
        .addClass('form-actions')
        .css('text-align', 'right')
        .append($yes)
        .append($no)
        .appendTo($sidebar);
    
    // opens dialog
    $dlg.dialog({
        width: 300,
        autoResize: false,
        modal: true,
        resizable: false,
        title: "Confirm"
    });
}

/**
 *  Helps to Log In in the system
 */
function login(options){
    this.isEmpty = function($el){
        return !$el || $el.size()==0 || $el.val()=='';
    }
    
    if(this.isEmpty(options.login) || this.isEmpty(options.password)) {
        Notifier.warning('Please, fill login and password.', 'Wrong credentials!')
        return;
    }
    
    $.ajax({
        url: options.url+"/j_spring_security_check",
        data: {
            j_username: options.login.val(),
            j_password: options.password.val()
        },
        type: "POST",
        success: function(result) {
            var resp = handleResponse(result)
            if(resp.result) {
                if(options.doReditect && resp.redirectUrl){
                    window.location.href = options.url+'/'+resp.redirectUrl;
                }
                reloadMenu();
            } else {
                options.password.val('');
            }
        }
    });
}

/**
 *  Helps to Log Out from the system
 */
function logout(url){
    $.ajax({
        url: url+"/j_spring_security_logout",
        type: "POST",
        success: function(result) {
            var resp = handleResponse(result)
            if(resp.result) reloadMenu();
        }
    });
}


(function( $ ){
    $.myAjax = function(options) {
        var success = options.success;
        options.success = function(data, textStatus, jqXHR){
            if(!handleResponse(data)) return;
            if(success) success(data, textStatus, jqXHR);
        }
        this.ajax(options);
    };
})( jQuery );

